﻿using Domain.Entitys.Base_Manage;
using Domain.IRepositorys.Base_Manage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace EFCore.Repositorys.Base_Manage
{
    public class DicttypeRepository : IDicttypeRepository
    {
        private readonly MyDbContext myDbContext;

        public DicttypeRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        /// <summary>
        /// 查询总条数
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Getsys_Dict_TypesCount(string Dict_name, string Dict_type, char Status)
        {
            //只查询没有被删除的数据
            IQueryable<Sys_Dict_Type> query = myDbContext.Sys_Dict_Type.Where(d => d.Del_flag == '0');
            //条件筛选
            if (!string.IsNullOrEmpty(Dict_name))
            {
                query = query.Where(d => d.Dict_name.Contains(Dict_name));
            }
            if (!string.IsNullOrEmpty(Dict_type))
            {
                query = query.Where(d => d.Dict_type.Contains(Dict_type));
            }
            if (Status == '0' || Status == '1')
            {
                query = query.Where(d => d.Status == Status);
            }
            return query.Count();
        }
        /// <summary>
        /// 分页查询、条件查询
        /// </summary>
        /// <param name="Dict_name"></param>
        /// <param name="Dict_type"></param>
        /// <param name="Status"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Sys_Dict_Type> Getsys_Dict_TypesFyWhere(string Dict_name, string Dict_type, char Status, int currentPage, int pageSize)
        {
            //只查询没有被删除的数据
            IQueryable<Sys_Dict_Type> query = myDbContext.Sys_Dict_Type.Where(d => d.Del_flag == '0');
            //条件筛选
            if (!string.IsNullOrEmpty(Dict_name))
            {
                query = query.Where(d => d.Dict_name.Contains(Dict_name));
            }
            if (!string.IsNullOrEmpty(Dict_type))
            {
                query = query.Where(d => d.Dict_type.Contains(Dict_type));
            }
            if (Status == '0' || Status == '1')
            {
                query = query.Where(d => d.Status == Status);
            }
            //分页查询
            var type = query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
            return type;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Postsys_Dict_TypesAdd(Sys_Dict_Type type)
        {
            await myDbContext.Sys_Dict_Type.AddAsync(type);
            return await myDbContext.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Putsys_Dict_TypesDelete(Sys_Dict_Type type)
        {
            Sys_Dict_Type dicttype = await myDbContext.Sys_Dict_Type.FindAsync(type.Dict_id);
            dicttype.Del_flag = type.Del_flag;
            return await myDbContext.SaveChangesAsync();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Putsys_Dict_TypesUpdate(Sys_Dict_Type type)
        {
            Sys_Dict_Type dicttype = myDbContext.Sys_Dict_Type.Find(type.Dict_id);
            dicttype.Dict_name = type.Dict_name;
            dicttype.Status = type.Status;
            dicttype.Remark = type.Remark;
            dicttype.Update_by = type.Update_by;
            dicttype.Update_time = type.Update_time;
            myDbContext.Sys_Dict_Type.Update(dicttype);
            return myDbContext.SaveChangesAsync();
        }
    }
}
